<?php
define ( 'IN_UCHOME', '1' );
include_once 'common.php';

$email = $_REQUEST ['e'];
$pwmd5 = $_REQUEST ['p'];

$auth = checkAuth ( $email, $pwmd5 );
$arr = array (
		"status" => 0 
);

if ($_GET ["f"] == "logintest") {
	echo json_encode ( $auth );
	exit ();
}
if (! $auth ['status']) {
	$arr ['err'] = $auth ['err'];
	$arr ['msg'] = $auth ['msg'];
	echo json_encode ( $arr );
	exit ();
}

$user = $auth ['user'];
$_SGLOBAL ['supe_uid'] = $user ['uid'];

$result = "";

$album = "picture";
$uid = $user ['uid'];
$username = $user ['username'];
$title = '';
$topic = '';
$orgUrl = "";
$referUrl = "";

if ($_POST ["f"] == "upload") {
	$album = $_POST ['ugt'] ? $_POST ['ugt'] : "";
	$referUrl = $_POST ['r'] ? $_POST ['r'] : "";
	$referUrl = unescape ( $referUrl );
	$title = $_POST ['image_name'] ? $_POST ['image_name'] : "";
	$title = unescape ( $title );
	$topic = 'postupload';
	// file_put_contents("./post.txt", print_r($_POST,true));exit;
	$result = pic_save ( $_FILES ['_file'], $album, $uid, $username, $title, $topic, $orgUrl, $referUrl );
} else if ($_GET ["f"] == "upload") {
	$album = $_GET ['ugt'] ? $_GET ['ugt'] : "";
	$topic = 'getupload';
	$orgUrl = $_GET ['url'] ? $_GET ['url'] : "";
	$referUrl = $_GET ['r'] ? $_GET ['r'] : "";
	
	$result = stream_save ( file_get_contents ( $_GET ['url'] ), $album, $uid, $username, $title, $topic, $orgUrl, $referUrl );
} else if ($_GET ["f"] == "uploadify") {
	$title = '';
	$topic = 'uploadify';
	$result = pic_save ( $_FILES ['_file'], $album, $uid, $username, $title, $topic, $orgUrl, $referUrl );
} else if ($_GET ["f"] == "drag-drop") {
	$topic = 'drag-drop';
	$result = pic_save ( $_FILES ['_file'], $album, $uid, $username, $title, $topic, $orgUrl, $referUrl );
} else if ($_GET ["f"] == "xheditor") {
	$localName = "";
	if (isset ( $_SERVER ['HTTP_CONTENT_DISPOSITION'] ) && preg_match ( '/attachment;\s+name="(.+?)";\s+filename="(.+?)"/i', $_SERVER ['HTTP_CONTENT_DISPOSITION'], $info )) { // HTML5上传
		
		$localName = urldecode ( $info [2] );
	}
	$title = $localName;
	$topic = 'xheditor';
	// print_r($localName);exit;
	$result = stream_save ( file_get_contents ( "php://input" ), $album, $uid, $username, $title, $topic, $orgUrl, $referUrl );
	
	$return = array (
			"err" => "",
			"msg" => "" 
	);
	if (! is_array ( $result )) {
		$return ['err'] = "图片保存失败,原因：" . $result;
		echo json_encode ( $return );
		exit ();
	}
	
	$ext = ! empty ( $result ['thumbArr'] ['800'] ) ? ".800.jpg" : "";
	$result ['src'] = $_SC ['attachurl'] . $result ['filepath'] . $ext;
	
	$thumbext = ! empty ( $result ['thumbArr'] ['thumb'] ) ? ".thumb.jpg" : "";
	$result ['thumb'] = $_SC ['attachurl'] . $result ['filepath'] . $thumbext;
	$result ['filepath'] = $_SC ['attachdir'] . $result ['filepath'] . $ext;
	
	list ( $width, $height, $type, $attr ) = getimagesize ( $result ['filepath'] );
	$arr ['status'] = 1;
	$arr ['url'] = $result ['src'] . "?t=" . time ();
	$arr ['thumb'] = $result ['thumb'] . "?t=" . time ();
	$arr ['width'] = $width;
	$arr ['height'] = $height;
	
	$return ['msg'] = $arr;
	
	echo json_encode ( $return );
	exit ();
}

if (! is_array ( $result )) {
	$arr ['err'] = "图片保存失败,原因：" . $result;
	echo json_encode ( $arr );
	exit ();
}

$ext = ! empty ( $result ['thumbArr'] ['800'] ) ? ".800.jpg" : "";
$result ['src'] = $_SC ['attachurl'] . $result ['filepath'] . $ext;

$thumbext = ! empty ( $result ['thumbArr'] ['thumb'] ) ? ".thumb.jpg" : "";
$result ['thumb'] = $_SC ['attachurl'] . $result ['filepath'] . $thumbext;
$result ['filepath'] = $_SC ['attachdir'] . $result ['filepath'] . $ext;

list ( $width, $height, $type, $attr ) = getimagesize ( $result ['filepath'] );
$arr ['status'] = 1;
$arr ['pic'] = $result ['src'] . "?t=" . time ();
$arr ['thumb'] = $result ['thumb'] . "?t=" . time ();
$arr ['width'] = $width;
$arr ['height'] = $height;
echo json_encode ( $arr );
exit ();
?>